package org.example.quartz;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.SysUser;
import org.example.serivce.ISysUserService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Random;

@Slf4j
@Component
public class TestQuartz extends QuartzJobBean {

    @Autowired
    ISysUserService iSysUserService;

    private Random random = new Random();

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            SysUser user = new SysUser();
            user.setName("测试");
            user.setId(DateUtil.current() + random.nextInt(10000));
            iSysUserService.saveUser(user);
            PageInfo<SysUser> pageInfo = iSysUserService.selectByPage(2, 10);
            for (SysUser sysUser : pageInfo.getList()) {
//                log.info(JSON.toJSONString(sysUser));
            }
//        log.info("Hello Job执行时间: " + DateUtil.now());
            DruidDataSource dataSource = SpringUtil.getBean("dataSource");
            log.info("Mybatis selectByPage PageNum={},PageSize={},Total={},pages={}",pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getPages());
        } catch (Exception e) {
            log.error("Hello Job执行失败: " + DateUtil.now(), e);
        }
    }
}
